using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using Bosphorus.Integration.Jira.Thor.Client.PlugIn.Interop.Core;
using Bosphorus.Integration.Jira.Thor.Client.PlugIn.Resource.String;

namespace Bosphorus.Integration.Jira.Thor.Client.PlugIn.Core.PlugIn
{
    [ComVisible(true),
     Guid("5870B3F1-8393-4c83-ACED-1D5E803A4F2B"),
     ClassInterface(ClassInterfaceType.None)]
    public abstract class AbstractPlugIn : IBugTraqProvider, IBugTraqProvider2, IPlugIn
    {
        private readonly string linkText;

        protected AbstractPlugIn(string linkText)
        {
            this.linkText = linkText;
            Initialize();
        }

        protected AbstractPlugIn()
            : this(Message.FORM_OPEN_TITLE)
        {
            
        }

        bool IBugTraqProvider2.ValidateParameters(IntPtr hParentWnd, string parameters)
        {
            return Validate(parameters);
        }

        string IBugTraqProvider2.GetLinkText(IntPtr hParentWnd, string parameters)
        {
            return linkText;
        }

        string IBugTraqProvider2.GetCommitMessage(IntPtr hParentWnd, string parameters, string commonRoot, string[] pathList, string originalMessage)
        {
            return ((IBugTraqProvider2)this).GetCommitMessage2(hParentWnd, parameters, string.Empty, commonRoot, pathList, originalMessage);
        }

        string IBugTraqProvider2.GetCommitMessage2(IntPtr hParentWnd, string parameters, string commonURL, string commonRoot, string[] pathList, string originalMessage)
        {
            return this.GetCommitMessage(parameters, originalMessage);
        }

        string IBugTraqProvider2.OnCommitFinished(IntPtr hParentWnd, string commonRoot, string[] pathList, string logMessage, int revision)
        { 
            CommitFinishedEventArgs commitFinishedEventArgs = new CommitFinishedEventArgs();
            commitFinishedEventArgs.LogMessage = logMessage;
            OnCommitFinished(this, commitFinishedEventArgs);
            
            return string.Empty;
        }

        bool IBugTraqProvider2.HasOptions()
        {
            return false;
        }

        string IBugTraqProvider2.ShowOptionsDialog(IntPtr hParentWnd, string parameters)
        {
            return string.Empty;
        }



        bool IBugTraqProvider.ValidateParameters(IntPtr hParentWnd, string parameters)
        {
            return ((IBugTraqProvider2)this).ValidateParameters(hParentWnd, parameters);
        }

        string IBugTraqProvider.GetLinkText(IntPtr hParentWnd, string parameters)
        {
            return ((IBugTraqProvider2)this).GetLinkText(hParentWnd, parameters);
        }

        string IBugTraqProvider.GetCommitMessage(IntPtr hParentWnd, string parameters, string commonRoot, string[] pathList, string originalMessage)
        {
            return ((IBugTraqProvider2) this).GetCommitMessage(hParentWnd, parameters, commonRoot, pathList, originalMessage);
        }


        public virtual void Initialize()
        {
            OnCommitFinished += delegate { };
        }

        public abstract bool Validate(string parameters);

        public abstract string GetCommitMessage(string parameters, string originalMessage);

        public event EventHandler<CommitFinishedEventArgs> OnCommitFinished;

    }
}